﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace inf_1007_devoir3
{
    public partial class frmRetourLocation : Form
    {
        private Location location;
        private frmListeLocations frmListe;
        public frmRetourLocation(frmListeLocations frmListe, Location location)
        {
            this.frmListe = frmListe;
            this.location = location;
            InitializeComponent();
        }

        private void frmRetourLocation_Load(object sender, EventArgs e)
        {
            txtNoPermis.Text = location.getClient().getNoPermisConduire();
            txtDescClient.Text = location.getClient().ToStringComplet();
            txtNoVehicule.Text = location.getVehicule().getNoLocation().ToString();
            txtDescVehicule.Text = location.getVehicule().ToStringComplet();
            txtKilometrage.Text = location.getKilometrageDepart().ToString();
        }

        private bool valider()
        {
            error.Clear();
            bool valide = true;

            double montantDegat = -1;

            try
            {
                montantDegat = Convert.ToDouble(txtMontant.Text.Trim());
            }
            catch (Exception e)
            {
                error.SetError(txtMontant, "Veuillez entrer un montant au format numérique.");
                valide = false;
            }

            int kilometrage = -1;

            try
            {
                kilometrage = Convert.ToInt32(txtKilometrage.Text.Trim());
            }
            catch (Exception e)
            {
                error.SetError(txtKilometrage, "Veuillez entrer un kilométrage au format numérique.");
                valide = false;
            }

            if (kilometrage < location.getKilometrageDepart())
            {
                error.SetError(txtKilometrage, "Veuillez entrer un kilométrage égal ou supérieur au kilométrage au départ.");
                valide = false;
            }

            return valide;
        }

        private void btnRetourner_Click(object sender, EventArgs e)
        {
            if (valider())
            {
                CtrlLocation.getInstance().passerAuPaiementRetour(frmListe,
                                                                  this,
                                                                  location,
                                                                  Convert.ToDouble(txtMontant.Text.Trim()),
                                                                  txtDescDegats.Text.Trim(),
                                                                  Convert.ToInt32(txtKilometrage.Text.Trim()));
            }
        }

        private void btnAnnuler_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
